<template>
    <div class="main">
      <p>当前标识符为:{{num}}</p>
      <p>当前openid为:{{openidx}}</p>
      <p>当前拿到openid时间为:{{nowtime}}</p>
    </div>
</template>

<script>
    export default {
        name: 'main',
        data() {
            return {
              num:"",
              nowtime:"",
              openidx:"",
            }
        },
        mounted() {
          var that =this;
          var myDate = new Date();
          myDate.toLocaleDateString();
          myDate.toLocaleTimeString();

          if(localStorage.openidx){
            //存在则继续往下执行
            that.nowtime  = myDate.toLocaleTimeString();
            that.openidx = localStorage.openidx;
            that.num = localStorage.randstrzzzz;

              $.ajax({
                  type:"GET",
                  url:"http://group.beautyfarm.com.cn/api/logOpenid",
                  dataType:"json",
                  data:{
                      "openid":localStorage.openidx,
                      "randstr":localStorage.randstrzzzz,
                      "access_token":localStorage.access_tokenx,
                      "dist":'继续执行',
                  },
                  success:function (res) {
                  }
              })

          }else{
            //不存在则执行
            var now_url = window.location.href;
            var url_param = window.location.search;
            //判断链接中是否有参数
            if(url_param.indexOf('openid') > 0){
              var params = url_param.substr(1).split('&');
              for(var i=0;i < params.length;i++){
                var temp = params[i].split("=");
                if(temp[0] == 'openid'){
                  localStorage.openidx = temp[1];
                }else if(temp[0] == 'access_token'){
                  localStorage.access_tokenx=temp[1];
                }
              }
              that.nowtime  = myDate.toLocaleTimeString();
              that.openidx = localStorage.openidx;
              that.num = localStorage.randstrzzzz;

              $.ajax({
                type:"GET",
                url:"http://group.beautyfarm.com.cn/api/logOpenid",
                dataType:"json",
                data:{
                    "openid":localStorage.openidx,
                    "randstr":localStorage.randstrzzzz,
                    "access_token":localStorage.access_tokenx,
                    "dist":'首次获取',
                },
                success:function (res) {
                }
              })
            }else{
              //链接中没有则通过美丽田园方指定授权链接获取
              var basic_url = 'http://bfwechat.beautyfarm.com.cn/Url.aspx?scope=snsapi_base&url=';
              var randstrzzzz = localStorage.randstrzzzz ? localStorage.randstrzzzz : that.randomString(8);
              localStorage.randstrzzzz= randstrzzzz;
              if(now_url.indexOf('?') >= 0){
                var return_url = now_url+'&rstr='+randstrzzzz;
              }else{
                var return_url = now_url+'?rstr='+randstrzzzz;
              }
              location.href = basic_url+encodeURIComponent(return_url);
            }
          }
        },
        methods: {
          randomString(len){
            len = len || 32;
            var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz012345678';
            var maxPos = $chars.length;
            var pwd = '';
            for (var i = 0; i < len; i++) {
              pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
            }
            return pwd;
          },
        }
    }
</script>

<style scoped>
  .main {
    width: 100vw;
    height:100wh;

  }
  .main p{
    width: 100%;
    text-indent: 1rem;
    margin-top:.5rem;
  }
  .main p:nth-child(1) {
    margin-top:2rem;
  }
</style>
